package com.yijianonghu.controller.wxwebsite.search;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yijianonghu.dal.model.SysBlog;
import com.yijianonghu.service.wxwebsite.search.SearchService;
import com.yijianonghu.util.Constants;
import com.yijianonghu.util.exceptionhandler.ControllerExceptionHandler;
import com.yijianonghu.util.result.MessageCode;
import com.yijianonghu.util.result.Result;

@Controller
@RequestMapping(value=Constants.WXConstants.WX_PREFIX+"search/", method=RequestMethod.POST)
public class SearchController {

	private static final Logger logger=LoggerFactory.getLogger(SearchController.class);
	
	@Autowired
	private ControllerExceptionHandler exceptionHandler;
	
	@Autowired
	private SearchService searchService;
	
	@RequestMapping("searchForProductAndService")
	public @ResponseBody Result searchForProductAndService(String name, String orderBy, Integer offset, Integer limit){
		Result result=new Result();
		List<SysBlog> list=null;
		try {
			list=searchService.searchForProduct(name, orderBy, offset, limit);
		} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException e) {
			exceptionHandler.handleException(logger, "获取搜索结果失败！", result, MessageCode.SYSTEM_BUSY, e);
		}
		if(list!=null && (!list.isEmpty())){
			result.setInfor(list);
			result.setSuccess(true);
		}
		return result;
	}
}
